package com.huike.report.service.impl;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.huike.report.domain.vo.CluesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.vo.IndexStatisticsVo;
import com.huike.clues.mapper.SysDeptMapper;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.mapper.TbActivityMapper;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.common.utils.SecurityUtils;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.report.domain.vo.IndexVo;
import com.huike.report.mapper.ReportMapper;
import com.huike.report.service.ReportService;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private TbContractMapper contractMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private TbClueMapper clueMapper;

    @Autowired
    private TbActivityMapper activityMapper;

    @Autowired
    private TbBusinessMapper businessMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private TbAssignRecordMapper assignRecordMapper;
    
    @Autowired
    private ReportMapper reportMapper;

    @Override
    public IndexVo getIndex(IndexStatisticsVo request) {
        Long deptId= request.getDeptId();
        TbAssignRecord tbAssignRecord=new TbAssignRecord();
        tbAssignRecord.setLatest("1");
        assignRecordMapper.selectAssignRecordList(tbAssignRecord);
        return null;
    }

    @Override
    public List<Map<String, Object>> salesStatisticsForIndex(IndexStatisticsVo request) {
        List<Map<String, Object>> list= contractMapper.contractStatisticsByUser(request);
        for (Map<String, Object> datum : list) {
            Long deptId= (Long) datum.get("dept_id");
            if(deptId!=null){
                SysDept dept= deptMapper.selectDeptById(deptId);
                datum.put("deptName", dept.getDeptName());
            }
        }
        return list;
    }



    /**
     * 统计分析 线索统计 新增线索数量折线图
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public CluesVo getCluesStatistics(String beginCreateTime, String endCreateTime) {
        CluesVo cluesVo = new CluesVo();
        List<String> days = getDays(beginCreateTime, endCreateTime);
        cluesVo.setXAxis(days);

        List<Map<String, Object>> todayCount = reportMapper.selectTodayCount(beginCreateTime, endCreateTime);

        Map<String,Object> map1 = new HashMap<>();
        List<Integer> list1 = new ArrayList<>();
        map1.put("name","新增线索数量");

        Map<String,Object> map2 = new HashMap<>();
        List<Integer> list2 = new ArrayList<>();
        map2.put("name","线索总数量");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int sum = 0;
        for (String day : days) {
            try {
                Date parse = sdf.parse(day);
                Optional optional = todayCount.stream().filter(s -> s.get("date").equals(parse)).findFirst();
                if (optional.isPresent()){
                    Map<String, Object> map = (Map<String, Object>) optional.get();
                    list1.add(Integer.parseInt(map.get("num").toString()));
                    sum += Integer.parseInt(map.get("num").toString());
                }else {
                    list1.add(0);
                }
                list2.add(sum);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        map1.put("data",list1);
        map2.put("data",list2);

        List<Map<String,Object>> list = new ArrayList<>();
        list.add(map1);
        list.add(map2);

        cluesVo.setSeries(list);

        return cluesVo;
    }

    /**
     * 统计分析 客户统计 学科客户分布饼图
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public List<Map<String,Object>> getSubjectStatistics(String beginCreateTime, String endCreateTime) {
        //CluesVo cluesVo = new CluesVo();
        List<Map<String, Object>> list = reportMapper.selectSubjectStatistics(beginCreateTime, endCreateTime);
        for (Map<String, Object> map : list) {
            Object subject = map.get("subject");
            String name = reportMapper.selectName(subject.toString());
            map.put("subject",name);
        }
        //cluesVo.setData(list);
        return list;
    }

    //获取两个日期之间的所有日期
    public static List<String> getDays(String startTime, String endTime) {
        // 返回的日期集合
        List<String> days = new ArrayList<String>();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    private BigDecimal getRadio(Integer all,Long num) {
        if(all.intValue()==0){
            return new BigDecimal(0);
        }
		BigDecimal numBigDecimal = new BigDecimal(num);
		BigDecimal allBigDecimal = new BigDecimal(all);
		BigDecimal divide = numBigDecimal.divide(allBigDecimal,4,BigDecimal.ROUND_HALF_UP);
		return divide.multiply(new BigDecimal(100));
	}

}